<template>
  <div style="width: 100%;">
    <el-dialog title="批量添加" :visible.sync="dialogVisible" width="50%">
      <div>
        <div class="padt20">
          <span>*你可以下载设定好的批量导入的信息模板，输入内容后再上传</span>
          <span class="c-blue" @click="uploadXls">下载模板</span>
        </div>
        <div class="padt20">
          <el-upload
            ref="upload"
            class="upload-demo"
            :headers="{'Authorization':this.token}"
            :data="{companyId:this.companyId}"
            :on-success="uploadSuccess"
            :action="baseApi + importData.uploadUrl"
            multiple
            :limit="3"
            :auto-upload="false"
          >
            <el-button size="small" icon="el-icon-upload" type="primary" @click="submitUpload">点击上传</el-button>
            <span>*请上传后缀名为.xls,.xlsx的文件</span>
          </el-upload>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpload">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { Message } from "element-ui";
import { baseApi } from "@/utils/index";
import { getComanyId } from "@/utils/auth";
import { import_department } from "@/api/student";
export default {
  name: "batchImport",

  computed: {
    importData() {
      return this.$store.getters.importData;
	},
	...mapGetters(["token"])
  },
  created() {
    this.baseApi = baseApi();
	this.companyId = getComanyId();
	
  },
  data() {
    return {
      baseApi: "",
      dialogVisible: false,
      companyId: ""
    };
  },
  props: {
    importVisible: {}
  },
  watch: {
    importVisible() {
      this.dialogVisible = this.importVisible;
    },
    dialogVisible() {
      //点击了对话框“x"号,
      if (!this.dialogVisible) {
        this.closeDialog();
      } else {
      }
    }
  },
  methods: {
    uploadXls() {
      let urls = baseApi() + this.importData.importUrl;
      window.open(urls);
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    uploadSuccess(response, file, fileList) {
      let data = response;
      if (data.status == 1) {
        this.$emit("uploadSuccess", {});
        this.closeDialog();
      } else {
        let htmlMsg =
          '<h3 style="text-align:center;margin-top:0;">' +
          data.message +
          "</h3>";
        data.entity.forEach(item => {
          htmlMsg += '<p style="padding-bottom:10px;">' + item + "</p>";
        });

        Message({
          message: htmlMsg,
          type: "error",
          duration: 0,
          showClose: true,
          dangerouslyUseHTMLString: true
        });
      }
    },
    closeDialog() {
      this.$emit("closeDiag", false);
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.padt20 {
  padding-bottom: 20px;
}
.c-blue {
  color: #409eff;
  cursor: pointer;
}

.row-container {
  .el-col {
    margin-left: 20px;
    margin-top: 30px;
    height: 500px;
    border: 1px solid #ddd;
    padding: 30px;
    span {
      padding: 15px;
      font-size: 12px;
      color: #606266;
    }
    .c-black {
      color: #1d2026;
      padding-bottom: 10px;
    }
    p {
      font-size: 14px;
      color: #606266;
      padding-left: 15px;
      line-height: 23px;
    }

    .span-title {
      padding-right: 20px;
    }
    .buton-contain {
      margin: 50px 0 0 50px;
    }
    .title {
      font-size: 14px;
      font-weight: bold;
    }
    .img-contain {
      padding: 15px;
      padding-bottom: 0;
      img {
        max-width: 100%;
      }
    }
  }
}
</style>